package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import org.gdstash.character.GDCharUID;
import org.gdstash.db.DBShrine;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharShrineAct4Pane extends GDAbstractShrinePane {
    private JCheckBox cbAsterkarnValley;
    private JCheckBox cbMogdrogensShrine;
    private JCheckBox cbTombOfTheDamned;
    private JCheckBox cbGatesOfNecropolis;
    private JCheckBox cbBlackSepulcher;
    private JCheckBox cbBastionOfChaos;
    private JCheckBox cbTombWatchers;
    private JCheckBox cbSanctumOfTheImmortal;
    private int difficulty;
    private boolean changed;

    public GDCharShrineAct4Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbAsterkarnValley).addComponent(this.cbMogdrogensShrine).addComponent(this.cbTombOfTheDamned).addComponent(this.cbGatesOfNecropolis).addComponent(this.cbBlackSepulcher).addComponent(this.cbBastionOfChaos).addComponent(this.cbTombWatchers).addComponent(this.cbSanctumOfTheImmortal));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbAsterkarnValley)).addGroup(layout.createParallelGroup().addComponent(this.cbMogdrogensShrine)).addGroup(layout.createParallelGroup().addComponent(this.cbTombOfTheDamned)).addGroup(layout.createParallelGroup().addComponent(this.cbGatesOfNecropolis)).addGroup(layout.createParallelGroup().addComponent(this.cbBlackSepulcher)).addGroup(layout.createParallelGroup().addComponent(this.cbBastionOfChaos)).addGroup(layout.createParallelGroup().addComponent(this.cbTombWatchers)).addGroup(layout.createParallelGroup().addComponent(this.cbSanctumOfTheImmortal));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbAsterkarnValley, this.cbMogdrogensShrine);
        layout.linkSize(0, this.cbAsterkarnValley, this.cbTombOfTheDamned);
        layout.linkSize(0, this.cbAsterkarnValley, this.cbGatesOfNecropolis);
        layout.linkSize(0, this.cbAsterkarnValley, this.cbBlackSepulcher);
        layout.linkSize(0, this.cbAsterkarnValley, this.cbBastionOfChaos);
        layout.linkSize(0, this.cbAsterkarnValley, this.cbTombWatchers);
        layout.linkSize(0, this.cbAsterkarnValley, this.cbSanctumOfTheImmortal);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbMogdrogensShrine);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbTombOfTheDamned);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbGatesOfNecropolis);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbBlackSepulcher);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbBastionOfChaos);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbTombWatchers);
        layout.linkSize(1, this.cbAsterkarnValley, this.cbSanctumOfTheImmortal);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT4"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbAsterkarnValley == null) {
            (this.cbAsterkarnValley = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAsterkarnValley.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_ASTERKARN_VALLEY"));
        this.cbAsterkarnValley.setFont(fntCheck);
        if (this.cbMogdrogensShrine == null) {
            (this.cbMogdrogensShrine = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMogdrogensShrine.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_MOGDROGENS_SHRINE"));
        this.cbMogdrogensShrine.setFont(fntCheck);
        if (this.cbTombOfTheDamned == null) {
            (this.cbTombOfTheDamned = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTombOfTheDamned.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_TOMB_OF_THE_DAMNED"));
        this.cbTombOfTheDamned.setFont(fntCheck);
        if (this.cbGatesOfNecropolis == null) {
            (this.cbGatesOfNecropolis = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGatesOfNecropolis.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_GATES_OF_NECROPOLIS"));
        this.cbGatesOfNecropolis.setFont(fntCheck);
        if (this.cbBlackSepulcher == null) {
            (this.cbBlackSepulcher = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBlackSepulcher.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_BLACK_SEPULCHER"));
        this.cbBlackSepulcher.setFont(fntCheck);
        if (this.cbBastionOfChaos == null) {
            (this.cbBastionOfChaos = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBastionOfChaos.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_BASTION_OF_CHAOS"));
        this.cbBastionOfChaos.setFont(fntCheck);
        if (this.cbTombWatchers == null) {
            (this.cbTombWatchers = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTombWatchers.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_TOMB_OF_THE_WATCHERS"));
        this.cbTombWatchers.setFont(fntCheck);
        if (this.cbSanctumOfTheImmortal == null) {
            (this.cbSanctumOfTheImmortal = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSanctumOfTheImmortal.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_04_SANCTUM_OF_THE_IMMORTAL"));
        this.cbSanctumOfTheImmortal.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbAsterkarnValley.setSelected(selected);
        this.cbMogdrogensShrine.setSelected(selected);
        this.cbTombOfTheDamned.setSelected(selected);
        this.cbGatesOfNecropolis.setSelected(selected);
        this.cbBlackSepulcher.setSelected(selected);
        this.cbBastionOfChaos.setSelected(selected);
        this.cbTombWatchers.setSelected(selected);
        this.cbSanctumOfTheImmortal.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbAsterkarnValley.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_ASTERKARN_VALLEY);
        }
        if (this.cbMogdrogensShrine.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_MOGDROGENS_SHRINE);
        }
        if (this.cbTombOfTheDamned.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_TOMB_OF_THE_DAMNED);
        }
        if (this.cbGatesOfNecropolis.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_GATES_OF_NECROPOLIS);
        }
        if (this.cbBlackSepulcher.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_BLACK_SEPULCHER);
        }
        if (this.cbBastionOfChaos.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_BASTION_OF_CHAOS);
        }
        if (this.cbTombWatchers.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_TOMB_OF_WATCHERS);
        }
        if (this.cbSanctumOfTheImmortal.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_04_SANCTUM_OF_THE_IMMORTAL);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expFG = GDStashFrame.expansionForgottenGods;
        if (gdc != null) {
            expFG = (expFG || gdc.isForgottenGodsChar());
        }
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrined01.dbr", this.difficulty);
        this.cbAsterkarnValley.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrine_mogdrogen.dbr", this.difficulty);
        this.cbMogdrogensShrine.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrined02.dbr", this.difficulty);
        this.cbTombOfTheDamned.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrined03.dbr", this.difficulty);
        this.cbGatesOfNecropolis.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrined04.dbr", this.difficulty);
        this.cbBlackSepulcher.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinec07.dbr", this.difficulty);
        this.cbBastionOfChaos.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen07.dbr", this.difficulty);
        this.cbTombWatchers.setEnabled(info.active);
        this.cbTombWatchers.setVisible(info.exists || expFG);
        info = DBShrine.getShrineInfo("devotionshrines01.dbr", this.difficulty);
        this.cbSanctumOfTheImmortal.setEnabled(info.active && this.difficulty == 2);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_ASTERKARN_VALLEY)) {
                this.cbAsterkarnValley.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_MOGDROGENS_SHRINE)) {
                this.cbMogdrogensShrine.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_TOMB_OF_THE_DAMNED)) {
                this.cbTombOfTheDamned.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_GATES_OF_NECROPOLIS)) {
                this.cbGatesOfNecropolis.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_BLACK_SEPULCHER)) {
                this.cbBlackSepulcher.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_BASTION_OF_CHAOS)) {
                this.cbBastionOfChaos.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_TOMB_OF_WATCHERS)) {
                this.cbTombWatchers.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_04_SANCTUM_OF_THE_IMMORTAL)) {
                this.cbSanctumOfTheImmortal.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct4Pane.this.changed = true;
        }
    }
}
